package com.da.javatest.niuke.jianzhi;

import java.util.ArrayList;

import com.da.javatest.niuke.common.TreeNode;

/**
 * @author chenlida
 * @date 2020/10/26 16:30
 * https://www.nowcoder.com/practice/445c44d982d04483b04a54f298796288?
 * tpId=13&tags=&title=&diffculty=0&judgeStatus=0&rp=1
 */
public class C60Solution {
    private ArrayList<ArrayList<Integer>> result = new ArrayList<>();
    private int maxDeep;

    ArrayList<ArrayList<Integer>> Print(TreeNode pRoot) {
        maxDeep = maxDepth(pRoot);
        for (int i = 0; i < maxDeep; i++) {
            result.add(new ArrayList<>());
        }
        setResult(pRoot, 0);
        return result;
    }

    public void setResult(TreeNode node, int deep) {
        if (node != null) {
            setResult(node.left, deep + 1);
            result.get(deep).add(node.val);
            setResult(node.right, deep + 1);
        }
    }

    public int maxDepth(TreeNode root) {
        return root == null ? 0 : Math.max(maxDepth(root.left), maxDepth(root.right)) + 1;
    }
}
